'''
Created on Sep 17, 2012

@author: DuongThanh
'''
import csv 
from tool import createFilesForCRF
import commands 

inputFile = "../../data/lemmaFeaturesTrain_filtered.csv"

NUMBER_OF_FOLD = 5

file= open(inputFile)
lines = file.readlines()
count = len(lines)
eachFold = count / NUMBER_OF_FOLD
sumAcc = 0 
for i in range(0,NUMBER_OF_FOLD):
    tempTestFile = "../../data/tempTestFile"+str(i) + ".csv"
    tempTrainFile = "../../data/tempTrainFile"+str(i) + ".csv"

    # Fold i
    startLine = eachFold* i 
    endLine = eachFold * (i + 1) - 1   
    test = lines[startLine:endLine]
    train1 = []
    if (startLine > 1):
        train1 = lines[0:startLine-1]
    train2 = []
    if (endLine+1 < count - 1):
        train2 = lines[endLine + 1:count-1]
    train = train1 + train2 
    # OK, write it to file 
    file1 = open(tempTestFile,'w')
    for line in test:
        file1.write(line)
    file1.close()
    
    file2 = open(tempTrainFile,'w')
    for line in train:
        file2.write(line)
    file2.close()

    #continue

    # OK Run the script
    createFilesForCRF.createData(tempTrainFile, "../../data/train.data", 4)
    createFilesForCRF.createData(tempTestFile, "../../data/test.data", 4)
    
    createFilesForCRF.createTemplate("../../data/train.data")
    
    
    commands.getoutput("../../CRF++-0.57/crf_learn -a -p 2 -f 10 -c 0.065 ../../data/template ../../data/train.data ../../data/model")
    commands.getoutput("../../CRF++-0.57/crf_test -m ../../data/model ../../data/test.data > ../../data/tempOut.txt")
    
    
    createFilesForCRF.getFormattedOutput("../../data/CRFOutput_CrosValidation_"+str(i)+".csv", 100)
    
    # Now evaluate the result 
    resultFileName = "../../data/CRFOutput_CrosValidation_"+str(i)+".csv"
    file3 = open(resultFileName)
    spamreader = csv.reader(file3, delimiter=",")
    count= 0
    correctCount = 0 
    for row in spamreader:
        count +=1 
        value1= row[-1]
        value2 = row[-2]
        if (value1 == value2):
            correctCount+=1 
    file3.close()
    accuracy = correctCount * 1.0 / (count * 1.0)
    print (" ACCURACY = "+ str(accuracy)+"\n")
    #######################################
    sumAcc += accuracy
file.close()

print (" FINAL ACCURACY "+ str(sumAcc / (NUMBER_OF_FOLD * 1.0)))